//
//  ShowPieChartViewController.m
//  PieChart
//
//  Created by hukaiyin on 16/6/20.
//  Copyright © 2016年 HKY. All rights reserved.
//

#import "HKPieChartView.h"

#import "ShowPieChartViewController.h"

@interface ShowPieChartViewController ()

@property (nonatomic, strong) HKPieChartView *pieChartView;

@property (nonatomic, strong) UIView *backView; /**蒙版view*/
@end

@implementation ShowPieChartViewController

#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //    self.view.backgroundColor = [UIColor colorWithRed:0.118 green:0.133 blue:0.153 alpha:1.000];
    
    self.navigationItem.title = @"显示圆形进度条";
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupLabel];
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    // 蒙版
    self.backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    self.backView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5]; // 半透明灰色
    
    // 创建进度动画view
    CGFloat chartView_W = 150;
    CGRect chart_frame = CGRectMake((width - chartView_W)/2, chartView_W, chartView_W, chartView_W);
    self.pieChartView = [[HKPieChartView alloc] initWithFrame:chart_frame];
    _pieChartView.center = CGPointMake(self.view.center.x, 300);
    [_pieChartView updatePercent:self.percent animation:YES];
    
    [self.backView addSubview:self.pieChartView];
    
    
    
    [self.view addSubview:self.backView];
    
    __weak typeof(self) weakSelf = self;
    // 获取进度
    self.pieChartView.progressBlock = ^(NSString *progressStr) {
        if ([progressStr isEqualToString:@"100%"]) {
            // GCD 延时0.5s 移除进度view
            double delayInSeconds = 0.5;
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
            dispatch_after(popTime, dispatch_get_main_queue(), ^{
                [weakSelf hideProgressView]; 
            });
        }
    };
}

/**在view添加一个lable*/
- (void)setupLabel
{
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
    titleLabel.center = self.view.center;
    titleLabel.backgroundColor = [UIColor brownColor];
    titleLabel.text = @"跟换锁芯";
    [self.view addSubview:titleLabel];
}

/**删除进度view*/
- (void)hideProgressView 
{
    if (self.backView != nil) {
        [self.backView removeFromSuperview];
    }
    if (self.pieChartView != nil) {
        [self.pieChartView removeFromSuperview];
    }
}

#pragma mark - Getters & Setters

- (HKPieChartView *)pieChartView {
    if (!_pieChartView) {
        CGFloat width = 150;
        _pieChartView = [[HKPieChartView alloc]initWithFrame:CGRectMake((self.view.frame.size.width - width)/2, 150, width, width)];
        _pieChartView.center = CGPointMake(self.view.center.x, 300);
        [self.view addSubview:_pieChartView];
    }
    return _pieChartView;
}


@end
